<?php
/**
 * Created by PhpStorm.
 * User: kongqi
 * Date: 2019/11/30
 * Time: 3:28
 */

namespace App\Plugin\HostServer\Front;

use App\Plugin\HostServer\Models\PnHostSvCategory;
use App\Plugin\HostServer\Models\PnHostSvGood;

class GoodController extends BaseController
{
    public function good()
    {
        //遍历产品
        $pagesize = 20;
        $list = PnHostSvGood::checked()->sortDesc()->idDesc()->paginate($pagesize);

        $pager = $list->links();
        $title = config_cache_default('host_server.server_seo_title','所有服务器');
        $key = config_cache_default('host_server.server_seo_key',$title);
        $desc =config_cache_default('host_server.server_seo_desc',$title);
        $this->setSeo($title, $key, $desc);
        $data = [
            'list' => $list,
            'pager' => $pager,
            'category_name' => $title
        ];
        $this->setModelControllerView('index');
        return $this->display($data);
    }

    public function index($id = '')
    {

        $category = PnHostSvCategory::where('group_type', 'server')->checked()->find($id);
        if (empty($category)) {
            return $this->returnError(404, '找不到', plugin_route('hostserver.index'));

        }
        //遍历产品
        $pagesize = $category['pagesize'] ? $category['pagesize'] : 20;
        //判断是大类还是小类
        if ($category->parent_id == 0) {
            $list = PnHostSvGood::where('category_id', $id)->checked()->sortDesc()->idDesc()->paginate($pagesize);

        } else {
            $list = PnHostSvGood::where('config_level_id', $id)->checked()->sortDesc()->idDesc()->paginate($pagesize);

        }
        $pager = $list->links();
        $title = $category->seo_title ?: $category->name;
        $key = $category->seo_key ?: $category->seo_title;
        $desc = $category->seo_desc ?: $category->seo_title;
        $this->setSeo($title, $key, $desc);
        $data = [
            'list' => $list,
            'pager' => $pager,
            'category_name' => $category->name
        ];
        return $this->display($data);
    }


    public function show($id)
    {
        $good = PnHostSvGood::checked()->find($id);
        if (empty($good)) {
            return $this->returnError(404, '找不到', plugin_route('hostserver.index'));
        }
        $title = $good->seo_title ?: $good->name;
        $key = $good->seo_key ?: $good->seo_title;
        $desc = $good->seo_desc ?: $good->seo_title;
        $this->setSeo($title, $key, $desc);
        //找出系统类型
        $system_ids = $good->systems()->select('category_id')->groupBy('category_id')->get()->pluck('category_id')->toArray();
        $system = [];
        if (!empty($system_ids)) {
            $system = PnHostSvCategory::where('group_type', 'system')->whereIn('id', $system_ids)->get()->toArray();
        }

        $data = [
            'show' => $good,
            'system' => $system
        ];
        return $this->display($data);
    }
}